package com.shuoyue.ycgk.ui.mine.wrong;

import com.shuoyue.ycgk.base.BaseModel;
import com.shuoyue.ycgk.base.BasePresenter;
import com.shuoyue.ycgk.base.BaseResult;
import com.shuoyue.ycgk.base.BaseView;
import com.shuoyue.ycgk.entity.QuestionParent;
import com.shuoyue.ycgk.entity.ChapterType;
import com.shuoyue.ycgk.entity.SectionType;
import com.shuoyue.ycgk.net.RetrofitClient;
import com.shuoyue.ycgk.net.base.ApiSubscriber;
import com.shuoyue.ycgk.net.gsondefaultadapter.Optional;

import java.util.List;

import io.reactivex.Observable;

public interface WrongContract {
    interface View extends BaseView {
        void setTypeList(List<ChapterType> chapterSectionTypes);

        void setQuestions(List<QuestionParent> questionList);

        void clearSuc();

        void deleted(ChapterType chapter, SectionType sectionType);

        void changeStatue(int statue);
    }

    class Presenter extends BasePresenter<View> {
        Model model = new Model();

        public void getWrongTypes(int moduleId) {
            apply(model.getWrongTypes(moduleId))
                    .subscribe(new ApiSubscriber<Optional<List<ChapterType>>>(mView, this) {
                        @Override
                        public void onNext(Optional<List<ChapterType>> listOptional) {
                            super.onNext(listOptional);
                            mView.setTypeList(listOptional.getIncludeNull());
                        }
                    });
        }

        public void getWrongQuestions(Integer sectionId, int type) {
            apply(model.getWrongQuestions(sectionId, type))
                    .subscribe(new ApiSubscriber<Optional<List<QuestionParent>>>(mView, this) {
                        @Override
                        public void onNext(Optional<List<QuestionParent>> listOptional) {
                            super.onNext(listOptional);
                            mView.setQuestions(listOptional.getIncludeNull());
                        }
                    });
        }

        public void deleteAll(int moduleId) {
            apply(model.deleteAll(moduleId))
                    .subscribe(new ApiSubscriber<Optional<String>>(mView, this) {
                        @Override
                        public void onNext(Optional<String> listOptional) {
                            super.onNext(listOptional);
                            mView.clearSuc();
                        }
                    });
        }

        public void deleteWrongType(ChapterType chapterId, SectionType sectionId) {
            apply(model.deleteWrongType(chapterId == null ? null : chapterId.getChapterId(), sectionId == null ? null : sectionId.getSectionId()))
                    .subscribe(new ApiSubscriber<Optional<String>>(mView, this) {
                        @Override
                        public void onNext(Optional<String> listOptional) {
                            super.onNext(listOptional);
                            mView.deleted(chapterId, sectionId);
                        }
                    });
        }

        public void consolidate(Integer id, int statue) {
            apply(model.consolidate(id, statue))
                    .subscribe(new ApiSubscriber<Optional<String>>(mView, this) {
                        @Override
                        public void onNext(Optional<String> listOptional) {
                            super.onNext(listOptional);
                            mView.changeStatue(statue);
                        }
                    });
        }
    }

    class Model extends BaseModel {
        Observable<BaseResult<List<ChapterType>>> getWrongTypes(int moduleId) {
            return RetrofitClient.getInstance().getApi().getWrongTypes(moduleId);
        }

        Observable<BaseResult<List<QuestionParent>>> getWrongQuestions(Integer sectionId, int type) {
            return RetrofitClient.getInstance().getApi().getWrongQuestions(sectionId, type);
        }

        Observable<BaseResult<String>> deleteWrongType(Integer chapterId, Integer sectionId) {
            return RetrofitClient.getInstance().getApi().deleteWrongType(chapterId, sectionId);
        }

        Observable<BaseResult<String>> deleteAll(int modelId) {
            return RetrofitClient.getInstance().getApi().deleteAllWrong(modelId);
        }

        Observable<BaseResult<String>> consolidate(int memberPaperDetailId, int status) {
            return RetrofitClient.getInstance().getApi().consolidate(memberPaperDetailId, status);
        }
    }
}
